﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.StockManage
{
    public partial class RquisitionView : AuthBasePage
    {
        #region 属性定义
        private string m_strTbl = "BS_Rquisition";
        private string m_strMainKey = "BillID";

        private DataTable DtRquisitionData
        {
            get
            {
                if (null == Session["RquisitionData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["RquisitionData"];
                }
            }
            set
            {
                Session["RquisitionData"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("OrderNum");//1
            dt.Columns.Add("MaterialRequirementDetailID");//2
            dt.Columns.Add("MaterialRequirementID");//2
            dt.Columns.Add("WorkOrderNo");//3
            dt.Columns.Add("MaterialID");//4
            dt.Columns.Add("MaterialName");//5
            dt.Columns.Add("Specifications");//6
            dt.Columns.Add("Unit");//7
            dt.Columns.Add("AdviseNum");//8
            dt.Columns.Add("ResNum");//8
            dt.Columns.Add("Nums");//9
            dt.Columns.Add("MMNumber");
            dt.Columns.Add("SingleMMNumber");//11
            dt.Columns.Add("IsNew");//11
            dt.Columns.Add("IsDel");//11
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtRquisitionData = null;
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsRquisitionBusiness.GetDataByBillID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow drow = dt.Rows[0];
                    this.lblBillID.Text = id;
                    this.lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", drow["BillDate"]);
                    DataTable dtemp = DtRquisitionData.Copy();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow d = dtemp.NewRow();
                        d["BillDetailID"] = CommonMethod.GetString(dr["BillDetailID"]);
                        d["OrderNum"] = CommonMethod.GetString(dr["OrderNum"]);
                        d["MaterialRequirementDetailID"] = CommonMethod.GetString(dr["MaterialRequirementDetailID"]);
                        d["MaterialRequirementID"] = CommonMethod.GetString(dr["MaterialRequirementID"]);
                        d["WorkOrderNo"] = CommonMethod.GetString(dr["WorkOrderNo"]);
                        d["MaterialID"] = CommonMethod.GetString(dr["MaterialID"]);
                        d["MaterialName"] = CommonMethod.GetString(dr["MaterialName"]);
                        d["Specifications"] = CommonMethod.GetString(dr["Specifications"]);
                        d["Unit"] = CommonMethod.GetString(dr["Unit"]);
                        d["AdviseNum"] = CommonMethod.GetString(dr["AdviseNum"]);
                        d["ResNum"] = CommonMethod.GetString(dr["ResNum"]);
                        d["Nums"] = CommonMethod.GetString(dr["Nums"]);
                        d["MMNumber"] = CommonMethod.GetString(dr["MMNumber"]);
                        d["SingleMMNumber"] = CommonMethod.GetString(dr["SingleMMNumber"]);
                        d["IsNew"] = "-1";
                        d["IsDel"] = "-1";
                        dtemp.Rows.Add(d);
                    }
                    dtemp.AcceptChanges();
                    DtRquisitionData = dtemp.Copy();
                }
            }
            BindData();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                InitData();
            }
            UIHelper.RunScript(this.UpdatePanel1, "Refreashtab();");
        }

        private void BindData()
        {
            DataView dv = new DataView(DtRquisitionData);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }

    }
}